﻿@model NavigatorBarModel
@{
    Layout = "_Default";
    ViewData["Title"] = "Order";
}
@section css {

}
@section javascript {
    <script>
        var $messageName = $('#messageName');
        var $areaCode = $('#areaCode');
        var $sourceCode = $('#sourceCode');
        var $s_station = $('#s_station');
        var $d_station = $('#d_station');
        var $location = $('#location');
        var $priority = $('#priority');
        var $weight = $('#weight');
        var $state = $('#state');
        var $boxId = $('#boxId');
        var $wmsid = $('#wmsid');
        var $level = $('#level');
        var $dialogNew = $("#dialogNew");
        var $myModalLabel = $('#myModalLabel');
        var $TableGroup = $("#TableGroup");
        var $btn_query = $('#btn_query');
        var $btn_repeat = $('#btn_repeat');
        var $btn_add = $('.btn_add');
        var $txt_boxId = $('#txt_boxId');
        var $tb_edit = $('.tb_edit');
        var $editState = true;
        var $btnSubmit = $('#btnSubmit');
        var $dataForm = $('#dataForm');
        var $btn_delete = $('.btn_delete');
        var $id = $('#id');

        $(function () {

            //初始化Table
            var oTable = new TableInit();
            oTable.Init();

            //查询
            $btn_query.on('click', function () {
                $TableGroup.bootstrapTable('refresh');
            });

            //重置
            $btn_repeat.on('click', function () {
                $txt_boxId.val('');
                $TableGroup.bootstrapTable('refresh');
            });

            //添加
            $btn_add.on('click', function () {
                $editState = false;
                $myModalLabel.html('订单添加窗口');

                $dialogNew.find(".selectpicker").selectpicker('val', -1);
                $dialogNew.find(".form-control").val("");
                $id.val("0");
                $dialogNew.modal();
            });

            //编辑
            $tb_edit.on("click", function () {
                var data = $TableGroup.bootstrapTable('getSelections');
                if (data.length == 1) {
                    if (data[0].id != 0)
                        edit(data[0].id);
                } else {
                    toastr.error("请选中一行后编辑！");
                }
            });

            //编辑|添加数据提交
            $btnSubmit.click(function () {
                if (parseInt($areaCode.val()) == -1 || parseInt($sourceCode.val()) == -1) {
                    toastr.error("目标库区或来源库区不能为空");
                }
                else if (parseInt($messageName.val()) == -1 || parseInt($state.val()) == -1) {
                    toastr.error("请选择任务类型或订单状态");
                }
                else {
                    var requesturl = '/api/order/add';
                    if ($editState)
                        requesturl = '/api/order/edit';
                    $dialogNew.modal('hide');
                    Request("put", requesturl, serializeNotNull($dataForm.serialize()));
                }
            });
        });

        function serializeNotNull(serStr) {
            return serStr.split("&").filter(str => !str.endsWith("=")).join("&");
        }

        function Request(method, url, Data) {
            $.ajax({
                type: method,
                url: url,
                //contentType: 'application/json',
                data: Data,
                datatype: 'json',
                success: function (res) {
                    if (res.state == 200) {
                        $TableGroup.bootstrapTable('refresh');
                        toastr.success(res.message);
                    }
                    else {
                        toastr.error(res.message);
                    }
                },
                error: function (e) {
                    toastr.error(e.responseText);
                }
            });
        }

        //编辑函数
        function edit(id) {
            $editState = true;
            var itemInfo = DataArrar[id]
            if (itemInfo != null) {
                $myModalLabel.html('菜单编辑窗口');
                jQuery.each(itemInfo, function (i, val) {
                    $('#' + i + '').val(val);
                    $('#' + i + '').selectpicker('val', val)
                });
                $dialogNew.modal();
            }
        }

        $btn_delete.on("click", function () {
            var data = $TableGroup.bootstrapTable('getSelections');
            if (data.length > 0) {
                deleteRow(data);
            } else {
                toastr.error("请选中后操作！");
            }
        });

        function deleteTool(id) {
            deleteRow([{ "id": id }]);
        }

        function deleteRow(data) {
            bootbox.confirm({
                message: "确认删除吗？",
                buttons: {
                    confirm: {
                        label: '确认',
                        className: 'btn-primary'
                    },
                    cancel: {
                        label: '取消',
                        className: 'btn-default'
                    }
                },
                callback: function (result) {
                    if (result) {
                        $.ajax({
                            url: '/api/order',
                            type: "delete",
                            data: JSON.stringify(data),
                            contentType: 'application/json',
                            success: function (res) {
                                if (res.state == 200) {
                                    $TableGroup.bootstrapTable('refresh');
                                    toastr.success(res.message);
                                }
                                else {
                                    $TableGroup.bootstrapTable('refresh');
                                    toastr.error(res.message);
                                }
                            },
                            error: function (e) {
                                toastr.error(e.responseText);
                            }
                        });
                    }
                }
            });
        }

        var DataArrar = new Array();
        var TableInit = function () {
            var oTableInit = new Object();
            //初始化Table
            oTableInit.Init = function () {
                $('#TableGroup').bootstrapTable({
                    url: '/api/order/',           //请求后台的URL（*）
                    method: 'get',                      //请求方式（*）
                    toolbar: '#toolbar',                //工具按钮用哪个容器
                    striped: true,                      //是否显示行间隔色
                    cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: true,                   //是否显示分页（*）
                    sortable: false,                     //是否启用排序
                    sortOrder: "asc",                   //排序方式
                    queryParams: oTableInit.queryParams,//传递参数（*）
                    sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                    pageNumber: 1,                       //初始化加载第一页，默认第一页
                    pageSize: 10,                       //每页的记录行数（*）
                    pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                    search: false,                       //是否显示表格搜索，此搜索是客户端搜索，
                    strictSearch: true,
                    showColumns: true,                  //是否显示所有的列
                    showRefresh: true,                  //是否显示刷新按钮
                    minimumCountColumns: 2,             //最少允许的列数
                    clickToSelect: true,                //是否启用点击选中行
                    /*    height: 500,            */            //行高，如果没有设置height属性，表格自动根据记录条数决定表格高度
                    uniqueId: "id",                     //每一行的唯一标识，一般为主键列
                    showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
                    cardView: false,                    //是否显示详细视图
                    detailView: false,
                    columns: [
                        {
                            checkbox: true
                        }
                        , {
                            field: 'messageName',
                            title: '任务类型',
                            align: 'center'
                        }, {
                            field: 'boxId',
                            title: '箱号',
                            align: 'center'
                        }, {
                            field: 'wmsid',
                            title: 'WMSID',
                            align: 'center'
                        }, {
                            field: 'level',
                            title: '层位',
                            align: 'center'
                        }, {
                            field: 'weight',
                            title: '重量',
                            align: 'center'
                        }, {
                            field: 'areaCode',
                            title: '库区',
                            align: 'center'
                        }, {
                            field: 'sourceCode',
                            title: '来源库区',
                            align: 'center'
                        }
                        , {
                            field: 's_station',
                            title: '取货站台',
                            align: 'center'
                        }
                        , {
                            field: 'd_station',
                            title: '放货站台',
                            align: 'center'
                        }, {
                            field: 'location',
                            title: '货位',
                            align: 'center'
                        }, {
                            field: 'priority',
                            title: '优先级',
                            align: 'center'
                        }, {
                            field: 'state',
                            title: '状态',
                            align: 'center', formatter: function (value, row, index) {
                                if (value != null) {
                                    if (value == 0) {
                                        return "<span class='label label-wait'>待执行</span>";
                                    }
                                    if (value == 100) {
                                        return "<span class='label label-move '>执行中</span>";
                                    }
                                    if (value == 200) {
                                        return "<span class='label label-success'>已完成</span>";
                                    }
                                    if (value == 500) {
                                        return "<span class='label label-errer'>已取消</span>";
                                    }
                                }
                            }
                        }, {
                            field: 'createTime',
                            title: '创建时间',
                            align: 'center',
                            formatter: function (value, row, index) {
                                if (value != null) {
                                    return value.replace('T', ' ');
                                }
                            }
                        }, {
                            field: 'endTime',
                            title: '完成时间',
                            align: 'center'
                        },
                        {
                            field: "agvId", title: "操作", width: 200, align: "center", formatter: function (value, row, index) {
                                DataArrar[row.id] = row;
                                var strHtml = '<button type="button" class="btn btn-primary btn-xs" onclick="edit(' + row.id + ')">编辑</button>&nbsp;' +
                                    '<button type="button" class="btn btn-danger btn-xs" onclick="deleteTool(' + row.id + ')">删除</button>';
                                return strHtml;
                            }, edit: false
                        }
                    ]
                });
            };
            //得到查询的参数
            oTableInit.queryParams = function (params) {
                var temp = {                 //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    limit: params.limit,   //页面大小
                    offset: params.offset,  //页码
                    boxId: $("#txt_boxId").val()
                };
                return temp;
            };
            return oTableInit;
        };
    </script>
}

<section class="content">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h4 class="m-0 text-dark">订单管理</h4>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="@CoreConfiguration.DefaultMain">首页</a></li>
                        <li class="breadcrumb-item active">订单管理</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header">查询条件</div>
        <div class="card-body">
            <form class="form-inline">
                <div class="row">
                    <div class="form-group col-sm-6 col-lg-auto">
                        <label class="control-label">箱号</label>
                        <input type="text" class="form-control" id="txt_boxId">
                    </div>
                    <div class="form-group col-sm-6 col-lg-auto flex-sm-fill justify-content-sm-end align-self-sm-end">
                        <button style="margin:3px;" type="button" id="btn_query" class="btn btn-primary btn-fill float-left"><i class="fa fa-search" aria-hidden="true"></i><span>查询</span></button>
                        <button style="margin:3px;" type="button" id="btn_repeat" class="btn btn-primary btn-fill float-left"><i class="fa fa-repeat" aria-hidden="true"></i><span>重置</span></button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            查询结果
        </div>
        <div class="card-body">
            <div class="alert alert-warning" role="alert">
                <span>请谨慎修改</span>
            </div>
            <div id="toolbar" class="">
                <div class="toolbar btn-group">
                    <button type="button" class="btn btn-info btn_add"><i class="fa fa-plus mr-1" aria-hidden="true"></i><span>新增</span></button>
                    <button type="button" class="btn btn-primary tb_edit"><i class="fa fa-pencil mr-1" aria-hidden="true"></i><span>编辑</span></button>
                    <button type="button" class="btn btn-danger btn_delete"><i class="fa fa-remove mr-1" aria-hidden="true"></i><span>删除</span></button>
                </div>
            </div>
            <table id="TableGroup"></table>
            <div class="clearfix"></div>
        </div>
    </div>
</section>

@section modal {
    <div class="modal-header">
        <h5 class="modal-title" id="myModalLabel">订单编辑窗口</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="modal-body">
        <form class="form-inline" id="dataForm">
            <div class="form-row">
                <div class="form-group col-sm-6">
                    <input type="hidden" class="form-control" id="id" name="id" value="0" />
                    <label class="control-label" for="parentName">任务类型</label>
                    <div class="input-group flex-sm-fill">
                        <select class="selectpicker selectc" id="messageName" name="messageName" data-dropup-auto="false">
                            <option value="-1">请选择</option>
                            <option value="boxAnnounce">入库任务</option>
                            <option value="stockOut">出库任务</option>
                            <option value="movePosition">库内搬运</option>
                            <option value="carryPlatfrom">站台搬运</option>
                        </select>
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <input type="hidden" id="menuID" />
                    <label class="control-label" for="name">箱号</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control" id="boxId" name="boxId" placeholder="不可为空" maxlength="50" data-valid="true" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">WMSID</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control digits" id="wmsid" name="wmsid" data-default-val="10" placeholder="不为空" maxlength="8" data-valid="true" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="order">层位</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control digits" id="level" name="level" data-default-val="10" placeholder="可为空" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">目标库区</label>
                    <div class="input-group flex-sm-fill">
                        <select class="selectpicker selectc" id="areaCode" name="areaCode" data-dropup-auto="false">
                            <option value="-1">请选择</option>
                            <option value="A12">多穿库</option>
                            <option value="A13">阁楼库</option>
                            <option value="A14">重载库</option>
                            <option value="A20">WMS平台</option>
                            <option value="A21">WCS平台</option>
                        </select>
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">来源库区</label>
                    <div class="input-group flex-sm-fill">
                        <select class="selectpicker selectc" id="sourceCode" name="sourceCode" data-dropup-auto="false">
                            <option value="-1">请选择</option>
                            <option value="A12">多穿库</option>
                            <option value="A13">阁楼库</option>
                            <option value="A14">重载库</option>
                            <option value="A20">WMS平台</option>
                            <option value="A21">WCS平台</option>
                        </select>
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">取货站台</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control digits" id="s_station" name="s_station" data-default-val="10" placeholder="可为空" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">放货站台</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control digits" id="d_station" name="d_station" data-default-val="10" placeholder="可为空" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">货位</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control " id="location" name="location" placeholder="不为空" data-valid="true" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">优先级</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control digits" id="priority" name="priority" data-default-val="10" placeholder="不为空，默认为0，最大9999" maxlength="4" data-valid="true" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="icon">重量</label>
                    <div class="input-group flex-sm-fill">
                        <input type="text" class="form-control digits" id="weight" name="weight" data-default-val="10" placeholder="不为空，默认0" data-valid="true" />
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">订单状态</label>
                    <div class="input-group flex-sm-fill">
                        <select class="selectpicker selectc" id="state" name="state" data-dropup-auto="false">
                            <option value="-1">请选择</option>
                            <option value="0">待执行</option>
                            <option value="100">执行中</option>
                            <option value="200">已完成</option>
                            <option value="500">已取消</option>
                        </select>
                    </div>
                </div>
            </div>
        </form>
    </div>
}